//
//  ResultViewController.m
//  Xspcf
//
//  Created by 赵欢 on 2018/1/19.
//  Copyright © 2018年 小小算盘. All rights reserved.
//

#import "ResultViewController.h"
#import "SunRecordVC.h"

@interface ResultViewController ()
// 背景图
@property (weak, nonatomic) IBOutlet UIImageView *backImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *backImageHeight;
// 结果
@property (weak, nonatomic) IBOutlet UIImageView *resultImage;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;
// 文字
@property (weak, nonatomic) IBOutlet UILabel *wordOne;
@property (weak, nonatomic) IBOutlet UILabel *valueOne;
@property (weak, nonatomic) IBOutlet UILabel *wordTwo;
@property (weak, nonatomic) IBOutlet UILabel *valueTwo;
@property (weak, nonatomic) IBOutlet UILabel *wordThree;
@property (weak, nonatomic) IBOutlet UILabel *valueThree;
// 操作按钮
@property (weak, nonatomic) IBOutlet UIButton *bigButton;
@property (weak, nonatomic) IBOutlet UIButton *recordButton;


@end

@implementation ResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem.customView.hidden = YES;
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    [btn setFrame:CGRectMake(0, 0, 32, 22)];
    [btn addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated {
    if (self.type == Recharge) {
        self.title = @"充值结果";
        [self getRechardResultWithId:self.borrowId];
    }
    if (self.type == GetCash) {
        self.title = @"提现结果";
        [self getCashResultWithId:self.borrowId];
    }
}
- (void)finishAction {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)getCashResultWithId:(NSString *)cashId{
    //API_CADHRESULT
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_CADHRESULT];
    NSDictionary *dic = @{@"cashId":cashId};
    [HttpRequest postURL:url withParam:dic success:^(id requestDic, NSString *msg) {
        int status = [[requestDic objectForKey:@"status"]intValue];
        [self.backImageView setImage:[UIImage imageNamed:@"lose_bg"]];
        self.backImageHeight.constant = 275;
        self.resultLabel.text = [requestDic objectForKey:@"remark"];
        self.wordOne.text = @"提现金额";
        self.valueOne.text = [NSString stringWithFormat:@"%@元",[requestDic objectForKey:@"money"]];
        self.wordTwo.text = @"申请时间";
        self.valueTwo.text = [Sun timestamp:[[requestDic objectForKey:@"addTime"] intValue] ToTime:@"YY/MM/dd HH:mm"];
        self.wordThree.text = @"预计到账时间";
        NSString *arriveTime =[Sun timestamp:[[requestDic objectForKey:@"arriveTime"] intValue] ToTime:@"YY/MM/dd HH:mm"];
        self.valueThree.text = [arriveTime substringToIndex:arriveTime.length - 6];
        self.wordThree.hidden = NO;
        self.valueThree.hidden = NO;
        self.recordButton.hidden = YES;
        [self.bigButton setTitle:@"查看提现记录" forState:UIControlStateNormal];
        [self.bigButton addTarget:self action:@selector(openGetCashResordVC) forControlEvents:UIControlEventTouchUpInside];
        if (status == 0) {
            [self.resultImage setImage:[UIImage imageNamed:@"success_icon"]];
        }else if (status == 1){
            [self.resultImage setImage:[UIImage imageNamed:@"success_icon"]];
        }else if (status == 2){
            [self.resultImage setImage:[UIImage imageNamed:@"lose_icon"]];
        }
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];

}
- (void)getRechardResultWithId:(NSString *)rechardId{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_RECHARGERESULT];
    NSDictionary *dic = @{@"rechargeId":rechardId};
    [HttpRequest postURL:url withParam:dic success:^(id requestDic, NSString *msg) {
        int status = [[requestDic objectForKey:@"status"]intValue];
        self.wordOne.text = @"充值金额";
        self.valueOne.text = [NSString stringWithFormat:@"%@元",[requestDic objectForKey:@"money"]];
        self.wordTwo.text = @"充值时间";
        self.valueTwo.text = [Sun timestamp:[[requestDic objectForKey:@"addTime"] intValue] ToTime:@"YY/MM/dd HH:mm"];
        [self.recordButton setTitle:@"查看充值记录" forState:UIControlStateNormal];
        [self.recordButton addTarget:self action:@selector(openRechardRecordVC) forControlEvents:UIControlEventTouchUpInside];
        if (status == 0) {
            [self.resultImage setImage:[UIImage imageNamed:@"loding_icon"]];
            [self.backImageView setImage:[UIImage imageNamed:@"success_bg"]];
            self.resultLabel.text = @"充值处理中";
            self.wordThree.hidden = YES;
            self.valueThree.hidden = YES;
            [self.bigButton setTitle:@"去投资" forState:UIControlStateNormal];
            [self.bigButton addTarget:self action:@selector(gobuy) forControlEvents:UIControlEventTouchUpInside];
        }else if (status == 1){
            [self.resultImage setImage:[UIImage imageNamed:@"success_icon"]];
            [self.backImageView setImage:[UIImage imageNamed:@"success_bg"]];
            self.resultLabel.text = @"充值成功";
            self.wordThree.hidden = YES;
            self.valueThree.hidden = YES;
            [self.bigButton setTitle:@"去投资" forState:UIControlStateNormal];
            [self.bigButton addTarget:self action:@selector(gobuy) forControlEvents:UIControlEventTouchUpInside];
        }else if (status == 2){
            [self.backImageView setImage:[UIImage imageNamed:@"lose_bg"]];
            [self.resultImage setImage:[UIImage imageNamed:@"lose_icon"]];
            self.backImageHeight.constant = 305;
            self.resultLabel.text = @"充值失败";
            self.wordThree.text = @"失败原因";
            self.valueThree.text = [requestDic objectForKey:@"remark"];
            self.wordThree.hidden = NO;
            self.valueThree.hidden = NO;
            [self.bigButton setTitle:@"重新充值" forState:UIControlStateNormal];
            [self.bigButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        }
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}
- (void)openRechardRecordVC {
    SunRecordVC *recordVC = [[SunRecordVC alloc]init];
    recordVC.type = 2;
    [self.navigationController pushViewController:recordVC animated:YES];
}
- (void)openGetCashResordVC {
    SunRecordVC *recordVC = [[SunRecordVC alloc]init];
    recordVC.type = 1;
    [self.navigationController pushViewController:recordVC animated:YES];
}
- (void)back {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)gobuy {
        [self.tabBarController setSelectedIndex:1];
        [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
